package cn.wolfcode.web.controller;


import cn.wolfcode.domain.MessageBoardHjc;
import cn.wolfcode.service.IMessageBoardHjcService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IMessageReplyHjcService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.util.PermissionAnnotation;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.Date;

@Controller
@RequestMapping("/messageBoardHjc")
public class MessageBoardHjcController {

    @Autowired
    private IMessageBoardHjcService messageBoardHjcService;

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private IMessageReplyHjcService messageReplyHjcService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<MessageBoardHjc> pageInfo = messageBoardHjcService.query(qo);
        model.addAttribute("category",systemDictionaryItemService.selectBySn("business"));
        model.addAttribute("PageInfo", pageInfo);
        return "messageBoardHjc/list";
    }

    @RequestMapping("/show")
    public String show(Long id,Model model) {
        model.addAttribute("msg",messageBoardHjcService.selectById(id));
        model.addAttribute("items",messageReplyHjcService.selectById(id));
        return "messageBoardHjc/item";
    }

    @PermissionAnnotation(name="留言板新增/编辑",expression="messageBoardHjc:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(MessageBoardHjc messageBoardHjc) {
        if (messageBoardHjc.getId() == null) {
            messageBoardHjc.setCreateTimeHjc(new Date());
            messageBoardHjc.setReplystatusHjc(false);
            messageBoardHjcService.save(messageBoardHjc);
        } else {
            messageBoardHjcService.update(messageBoardHjc);
        }
        return "redirect:/messageBoardHjc/list";
    }

    @PermissionAnnotation(name="留言板新增/编辑",expression="messageBoardHjc:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("messageBoardHjc",messageBoardHjcService.get(id));
        }
        return "messageBoardHjc/input";
    }
}
